package com.lingnan.util;

import com.lingnan.config.HttpComponentsClientRestfulHttpRequestFactory;
import com.lingnan.pojo.User;
import com.lingnan.vo.AppKey;
import com.lingnan.vo.ResultVo;
import com.lingnan.vo.UserVo;
import net.minidev.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 梁兴达
 * @Date 2022/3/10 23:17:09
 * @Version 1.0
 */
public class HttpRequestUtil {
    public static ResponseEntity<JSONObject> httpRequestByMapAndUrlAndmethod(Map<String,String> bodyMap , String url, String method, RestTemplate restTemplate){
        MultiValueMap<String, String> map = new HttpHeaders();
        ResponseEntity<JSONObject> token = HttpRequestUtil.getToken(restTemplate);
        String token1 = (String) Objects.requireNonNull(token.getBody()).get("token");
        map.add("token",token1);
        ResponseEntity<JSONObject> exchange = null;
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(bodyMap,map);
        if (method.equals("get")){
            restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
            exchange = restTemplate.exchange(url,HttpMethod.GET,httpEntity,JSONObject.class);
        }else if (method.equals("post")){
            exchange = restTemplate.exchange(url,HttpMethod.POST,httpEntity,JSONObject.class);
        }

        return exchange;
    }

    public static ResponseEntity<com.alibaba.fastjson.JSONObject> verifyLogin( String url, HttpMethod method,RestTemplate restTemplate,ResultVo resultVo){
        System.out.println(resultVo);
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        ResponseEntity<com.alibaba.fastjson.JSONObject> responseEntity = restTemplate.exchange(url, method, RestTemplateUtils.getHeader(resultVo), com.alibaba.fastjson.JSONObject.class);
        System.out.println(responseEntity);
        System.out.println("ok !!!");
        com.alibaba.fastjson.JSONObject body = responseEntity.getBody();
        return responseEntity;
    }

    public static ResponseEntity<JSONObject> getToken(RestTemplate restTemplate){
        HashMap<String, String> map = new HashMap<>();
        //这是梁兴达的appkey和appsecret，如果换就改自己的
        map.put("appkey","10001");
        map.put("appsecret","95aed1cd5a0344a9a8aa785a026b7084");
        ResponseEntity<JSONObject> exchange = null;
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(map);
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        exchange = restTemplate.exchange("http://120.77.247.146:1281/dev/token",HttpMethod.POST,httpEntity,JSONObject.class);
        return exchange;
    }

    /**
     * 检查登录
     * @param userVo
     * @param url
     * @param restTemplate
     * @return
     */
    public static ResponseEntity<JSONObject> login(UserVo userVo, String url, RestTemplate restTemplate){
        HashMap<String, String> map = new HashMap<>();
        map.put("no",userVo.getNo());
        map.put("password",userVo.getPassword());
        ResponseEntity<JSONObject> exchange = null;
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(map);
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        exchange = restTemplate.exchange(url,HttpMethod.POST,httpEntity,JSONObject.class);
        return exchange;
    }

    public static ResponseEntity<JSONObject> reSetPassword(Map<String ,String> noMap,MultiValueMap<String, String> keyMap, String url, RestTemplate restTemplate){
//        HashMap<String, String> map = new HashMap<>();
//        map.put("appkey",keyMap.get("appkey"));
//        map.put("appsecret",keyMap.get("appsecret"));

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("no",noMap.get("no"));
        hashMap.put("password",noMap.get("password"));
        ResponseEntity<JSONObject> exchange;
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(hashMap,keyMap);
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        exchange = restTemplate.exchange(url,HttpMethod.POST,httpEntity,JSONObject.class);
        return exchange;
    }

}
